package com.xysq.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.xysq.util.PropertiesUtil;
import com.xysq.XYApplication;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ImageFragment extends Fragment {

    private static final String ARG_PARAM_URL = "param_url";

    private String mUrl;

    @InjectView(R.id.iv_icon)
    ImageView mImageView;

    public static ImageFragment newInstance(String param1) {
        ImageFragment fragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM_URL, param1);
        fragment.setArguments(args);
        return fragment;
    }

    public ImageFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mUrl = getArguments().getString(ARG_PARAM_URL);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!TextUtils.isEmpty(mUrl)) {
            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + mUrl, mImageView,
                    getImageLoadingListener());
        }
    }

    /**
     * Called when the fragment's activity has been created and this
     * fragment's view hierarchy instantiated.  It can be used to do final
     * initialization once these pieces are in place, such as retrieving
     * views or restoring state.  It is also useful for fragments that use
     * {@link #setRetainInstance(boolean)} to retain their instance,
     * as this callback tells the fragment when it is fully associated with
     * the new activity instance.  This is called after {@link #onCreateView}
     * and before {@link #onViewStateRestored(Bundle)}.
     *
     * @param savedInstanceState If the fragment is being re-created from
     *                           a previous saved state, this is the state.
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
    }

    private ImageLoadingListener getImageLoadingListener() {
        return new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                if (!isAdded()) {
                    return;
                }
                getActivity().setProgressBarIndeterminateVisibility(true);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                if (!isAdded()) {
                    return;
                }
                getActivity().setProgressBarIndeterminateVisibility(false);
                ToastUtil.showShort(getActivity(), R.string.txt_error_image_loading);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (!isAdded()) {
                    return;
                }
                getActivity().setProgressBarIndeterminateVisibility(false);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                if (!isAdded()) {
                    return;
                }
                getActivity().setProgressBarIndeterminateVisibility(false);
            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
